<template>
  <div class="app-container">

    <el-tabs v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="全部" name="qb" />
      <el-tab-pane v-for="item in langList" :key="item.id" :label="item.langCn" />
    </el-tabs>

    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="语言" prop="langId">
        <el-select v-model="queryParams.langId" placeholder="请选择">
          <el-option
            v-for="item in langList"
            :key="item.id"
            :label="item.langCn"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="标识" prop="flag">
        <el-input
          v-model="queryParams.flag"
          placeholder="请输入标识"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="总价格" prop="price">
        <el-input
          style="width: 100px"
          v-model="queryParams.minprice"
          placeholder="价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
        -
        <el-input
          style="width: 100px"
          v-model="queryParams.maxprice"
          placeholder="价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="赠送金额" prop="givePrice">-->
<!--        <el-input-->
<!--          v-model="queryParams.givePrice"-->
<!--          placeholder="请输入赠送金额"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="首冲价格" prop="firstPrice">-->
<!--        <el-input-->
<!--          v-model="queryParams.firstPrice"-->
<!--          placeholder="请输入首冲价格"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="划线价格" prop="originalPrice">
        <el-input
          style="width: 100px"
          v-model="queryParams.minoriginalPrice"
          placeholder="划线价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
        -
        <el-input
          style="width: 100px"
          v-model="queryParams.maxoriginalPrice"
          placeholder="划线价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否启用" prop="type">
        <el-select v-model="queryParams.status" placeholder="请选择">
          <el-option label="启用" value="1" />
          <el-option label="禁用" value="0" />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="创建时间" prop="createtime">-->
<!--        <el-input-->
<!--          v-model="queryParams.createtime"-->
<!--          placeholder="请输入创建时间"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="更新时间" prop="updatetime">-->
<!--        <el-input-->
<!--          v-model="queryParams.updatetime"-->
<!--          placeholder="请输入更新时间"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="19" style="text-align: right">
        <el-button
          icon="el-icon-search"
          size="mini"
          @click="handleShow"
        > </el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
<!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
    </el-row>

    <el-table v-loading="loading" :data="usableList" stripe @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
<!--      <el-table-column label="${comment}" align="center" prop="siteId" />-->
      <el-table-column label="语言" align="center" prop="vsLangInfo.langCn" />
      <el-table-column label="标题" align="center" prop="title" />
<!--      <el-table-column label="图片" align="center" prop="image" width="100">-->
<!--        <template slot-scope="scope">-->
<!--          <image-preview :src="scope.row.image" :width="50" :height="50"/>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="标识" align="center" prop="flag" />
      <el-table-column label="描述" width="200px" align="center" prop="desc" />
<!--      <el-table-column label="权益" align="center" prop="content" />-->
      <el-table-column label="总积分" align="center" prop="usable" />
      <el-table-column label="基础积分" align="center" prop="originalUsable" />
      <el-table-column label="赠送积分" align="center" prop="giveUsable" />
      <el-table-column label="总价格" align="center" prop="price" />
      <el-table-column label="赠送金额" align="center" prop="givePrice" />
<!--      <el-table-column label="首冲价格" align="center" prop="firstPrice" />-->
      <el-table-column label="划线价格" align="center" prop="originalPrice" />
      <el-table-column label="是否启用" align="center" prop="status" />
      <el-table-column label="排序" align="center" prop="weigh" />
<!--      <el-table-column label="创建时间" align="center" prop="createtime" />-->
<!--      <el-table-column label="更新时间" align="center" prop="updatetime" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改积分管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="语言" prop="langId">
          <el-select v-model="form.langId" placeholder="请选择">
            <el-option
              v-for="item in langList"
              :key="item.id"
              :label="item.langCn"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
<!--        <el-form-item label="图片" prop="image">-->
<!--          <image-upload v-model="form.image"/>-->
<!--        </el-form-item>-->
        <el-form-item label="标识" prop="flag">
          <el-input v-model="form.flag" placeholder="请输入标识" />
        </el-form-item>
        <el-form-item label="描述" prop="desc">
          <el-input v-model="form.desc" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="权益">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
        <el-form-item label="总价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入总价格" />
        </el-form-item>
        <el-form-item label="赠送金额" prop="givePrice">
          <el-input v-model="form.givePrice" placeholder="请输入赠送金额" />
        </el-form-item>
<!--        <el-form-item label="首冲价格" prop="firstPrice">-->
<!--          <el-input v-model="form.firstPrice" placeholder="请输入首冲价格" />-->
<!--        </el-form-item>-->
        <el-form-item label="划线价格" prop="originalPrice">
          <el-input v-model="form.originalPrice" placeholder="请输入划线价格" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listUsable, getUsable, delUsable, addUsable, updateUsable } from "@/api/usable/usable";
import {listLang} from "@/api/video/lang";

export default {
  name: "Usable",
  data() {
    return {
      activeName: 'qb',
      langList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // 积分管理表格数据
      usableList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        siteId: null,
        langId: '',
        title: null,
        image: null,
        flag: null,
        desc: null,
        content: null,
        usable: null,
        originalUsable: null,
        giveUsable: null,
        price: null,
        minprice: null,
        maxprice: null,
        minoriginalPrice: null,
        maxoriginalPrice: null,
        givePrice: null,
        firstPrice: null,
        originalPrice: null,
        status: null,
        weigh: null,
        createtime: null,
        updatetime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        siteId: [
          { required: true, message: "$comment不能为空", trigger: "blur" }
        ],
        langId: [
          { required: true, message: "语言不能为空", trigger: "blur" }
        ],
        title: [
          { required: true, message: "标题不能为空", trigger: "blur" }
        ],
        image: [
          { required: true, message: "图片不能为空", trigger: "blur" }
        ],
        flag: [
          { required: true, message: "标识不能为空", trigger: "blur" }
        ],
        desc: [
          { required: true, message: "描述不能为空", trigger: "blur" }
        ],
        content: [
          { required: true, message: "权益不能为空", trigger: "blur" }
        ],
        usable: [
          { required: true, message: "总积分不能为空", trigger: "blur" }
        ],
        originalUsable: [
          { required: true, message: "原始积分不能为空", trigger: "blur" }
        ],
        giveUsable: [
          { required: true, message: "赠送积分不能为空", trigger: "blur" }
        ],
        price: [
          { required: true, message: "总价格不能为空", trigger: "blur" }
        ],
        givePrice: [
          { required: true, message: "赠送金额不能为空", trigger: "blur" }
        ],
        firstPrice: [
          { required: true, message: "首冲价格不能为空", trigger: "blur" }
        ],
        originalPrice: [
          { required: true, message: "划线价格不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "是否启用:0=不启用,1=启用不能为空", trigger: "change" }
        ],
        weigh: [
          { required: true, message: "排序不能为空", trigger: "blur" }
        ],
        createtime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        updatetime: [
          { required: true, message: "更新时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getLangList();
    this.getList();
  },
  methods: {
    /** 查询语言包列表 */
    getLangList() {
      this.loading = true;
      listLang().then(response => {
        this.langList = response.rows;
        this.handleClick();
        this.loading = false;
      });
    },
    /** 切换页签查询对应数据 */
    handleClick() {
      if(this.activeName == 'qb') {
        this.queryParams.langId = ''
      } else {
        this.queryParams.langId = this.langList[this.activeName-1].id;
      }
      this.getList();
    },
    /** 查询积分管理列表 */
    getList() {
      this.loading = true;
      listUsable(this.queryParams).then(response => {
        this.usableList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        siteId: null,
        langId: null,
        title: null,
        image: null,
        flag: null,
        desc: null,
        content: null,
        usable: null,
        originalUsable: null,
        giveUsable: null,
        price: null,
        givePrice: null,
        firstPrice: null,
        originalPrice: null,
        status: null,
        weigh: null,
        createtime: null,
        updatetime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加积分管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getUsable(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改积分管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateUsable(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addUsable(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除积分管理编号为"' + ids + '"的数据项？').then(function() {
        return delUsable(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    handleShow() {
      this.showSearch = !this.showSearch;
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/usable/export', {
        ...this.queryParams
      }, `usable_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
